package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseTableListAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.FormChildDetail;
import com.isunland.managesystem.entity.TableColomn;
import com.isunland.managesystem.utils.BeanRefUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class FormChildListFragment extends Fragment {
  public static final String EXTRA_CHILD_LIST =
      "com.isunland.managesystem.Fragment.EXTRA_CHILD_LIST";
  //private ArrayList<String> mTitles;
  protected static final String SHOW_MARK = "@T";//显示标识
  public ArrayList<ArrayList<FormChildDetail>> mChildList;
  //private ArrayList<FormDetail> mList;
  private ListView mListView;

  public static FormChildListFragment newInstance(ArrayList<ArrayList<FormChildDetail>> childList) {
    FormChildListFragment fragment = new FormChildListFragment();
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CHILD_LIST, childList);
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      if (NavUtils.getParentActivityName(getActivity()) != null) {

        ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      }
    }

    //noinspection unchecked
    mChildList =
        (ArrayList<ArrayList<FormChildDetail>>) getArguments().getSerializable(EXTRA_CHILD_LIST);

    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_form_child_list, container, false);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.checkDetail);
    mListView = (ListView) view.findViewById(R.id.lv_child_form_list);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(getActivity(), FormChildActivity.class);
        ArrayList<ArrayList<FormChildDetail>> data = new ArrayList<>();
        data.add(mChildList.get(i));
        intent.putExtra(FormChildFragment.EXTRA_CHILD_LIST, data);
        startActivity(intent);
      }
    });
    return view;
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (mChildList == null || mChildList.size() == 0) return;
    //将name，value的map转换为对象。
    ArrayList<TableColomn> colomnList = new ArrayList<>();
    //动态配置
    LinkedHashMap<String, String> map = new LinkedHashMap<>();

    initData(colomnList, map);
    BaseTableListAdapter adapter =
        new BaseTableListAdapter<>(getActivity(), colomnList, map, new TableColomn());
    mListView.setAdapter(adapter);
  }

  public void initData(ArrayList<TableColomn> colomnList, LinkedHashMap<String, String> map) {
    //原来的接口返回了title字段，用于标题的显示，新版本中不需要标题了，所以要移除title对应的数据。
    for (ArrayList<FormChildDetail> formChildDetails : mChildList) {
      for (int i = 0; i < formChildDetails.size(); i++) {
        if (formChildDetails.get(i).getName().contains("title")) {
          formChildDetails.remove(i);
          i--;
        }
      }
    }

    for (ArrayList<FormChildDetail> formChildDetails : mChildList) {
      TableColomn colomn = new TableColomn();
      Map<String, String> valMap = new HashMap<>();

      for (int i = 0; i < formChildDetails.size(); i++) {
        valMap.put("colomn" + i, formChildDetails.get(i).getValue());
        String name = formChildDetails.get(i).getName();
        if (name.contains(SHOW_MARK)) {
          String key = name.substring(0, name.indexOf(SHOW_MARK));

          if (!map.containsKey(key)) {
            map.put(key, "colomn" + i);
          }
        }
      }
      BeanRefUtil.setFieldValue(colomn, valMap);
      colomnList.add(colomn);
    }

    //如果没有值，默认显示前三个字段
    if (map.size() == 0 && mChildList.get(0).size() > 2) {
      for (int i = 0; i < 3; i++) {
        map.put(mChildList.get(0).get(i).getName(), "colomn" + i);
      }
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }
}
